<?php



	/*
	 * All rights Reserved, Designed By www.youyacao.com <br>
	 * @Description:剧集 <br>
	 * @version 蜻蜓C影视追剧系统  <br>
	 * @author:成都市一颗优雅草科技有限公司  <br>
	 * 注意：使用我司开源源代码请遵循license文件的协议仅供个人非盈利使用，禁止用于其他的商业用途
	 * 需要商业用途或者定制开发等可访问songshu.youyacao.com  联系QQ:2853810243 422108995 23625059584
	 * 正版系统查询系统 zhengbancha.youyacao.com
	 */

namespace App\Admin\Controllers;

use App\Admin\Repositories\Episode;
use App\Models\Mp;
use App\Models\Type;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class EpisodeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Episode(), function (Grid $grid) {
//            dd(111);
            $grid->model()->orderByDesc('created_at');
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('blues');
            $grid->column('cover')->image('',100,100);
            $grid->column('cover_crosswise')->image('',100,100);
            $grid->column('sort')->editable();
            $grid->column('status')->switch();
            $grid->column('desc')->substr(0, 20);//->width('100px');
            $grid->column('type_id')->display(function ($type_id){
                $type=json_decode($type_id,true);
                $typemodel=Type::whereIn('id',$type)->pluck('title')->toArray();
                if(count($typemodel)==1){
                    return $typemodel[0];
                }else{
                    return implode('/',$typemodel);
                }
            });
//            $grid->column('unionid');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->async();
            $grid->quickSearch(['name'])->placeholder('可搜索剧集名称');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Episode(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('blues');
            $show->field('cover');
            $show->field('cover_crosswise');
            $show->field('sort');
            $show->field('status');
            $show->field('desc');
            $show->field('type_id');
            $show->field('unionid');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Episode(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->number('blues')->required();
            $form->image('cover')->required()->saveFullUrl()->autoUpload();
            $form->image('cover_crosswise')->required()->saveFullUrl()->autoUpload();
            $form->number('sort')->required();
            $form->radio('status')->options([0=>'待上架',1=>'上架'])->default(1)->required();
            $form->textarea('desc');
//            $form->text('type_id');
            $form->tree('type_id', '分类')
                ->setTitleColumn('title')
                ->treeState(false)
                ->nodes(\App\Models\Type::where('identify','video')->get()->toArray())
                ->saving(function ($v) {
                    // 转为 , 隔开的字符串
                    if(!empty($v)){
                        return json_encode($v);
                    }else{
                        return 0;
                    }
                })
                ->required();
            $form->multipleSelect('unionid')->options(Mp::pluck('name','id'))->saving(function ($v) {
                // 转为 , 隔开的字符串
                if(!empty($v)){
                    return json_encode($v);
                }else{
                    return 0;
                }
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
